//
//  SecurityPersonStatisticsVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/5/24.
//
// 【首页-保安管理】保安数据

import Foundation
import UIKit

class SecurityPersonStatisticsVC: SubLevelViewController {
    let sticsV = SecurityPersonStatisticsView()
    
    var param: SecurityParam = SecurityParam()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .bg
        navigationController?.navBarStyle(.white)
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        title = deptName ?? "保安管理"
        param.deptId = deptId
        param.dateType = dateEnum.rawValue
        param.date = date
        
        setupUI()
        
        reloadData()
    }
    
    func reloadData() {
        sticsV.reloadData(userName: "", date: date, type: dateEnum)
        fetchData()
    }
    
    func fetchData() {
        view.showToastActivity()

        // 履职
        API.getBaglBalzByDept(withParam: param) { responseModel in
            if responseModel.errorCode == .Success {
                DispatchQueue.main.async {
                    let lzrmc = (responseModel.model?.wdList != nil && responseModel.model!.wdList!.count > 0) ? responseModel.model?.wdList?[0].lzrmc : nil
                    self.sticsV.reloadData(userName: lzrmc, modal: responseModel.model)
                    self.view.hideToastActivity()
                }
            }
        }
    }
    
    func setupUI() {
        sticsV.baseTV.title = "保安管理"
        sticsV.sDelegate = self
        sticsV.customV.delegate = self
        sticsV.userBtn.delegate = self
        view.addSubview(sticsV)
        sticsV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}

extension SecurityPersonStatisticsVC: SecurityPersonStatisticsViewDelegate {
    func handleLzSelected(_ dept: BalzByDeptWdModel) {
        sticsV.reloadData(userName: dept.lzrmc)
        if let ownerId = dept.ownerId {
            let subVC = SecurityTopVC(withDeptId: deptId, deptName: deptName, date: date, dateEnum: dateEnum, ownerId: "\(ownerId)")
            navigationController?.pushViewController(subVC, animated: true)
        }
    }
}

extension SecurityPersonStatisticsVC: BSCustomDateViewDelegate {
    func didSelected(year: Int, quarter: Int?, month: Int?, day: Int?, dateEnum: CustomDateEnum) {
        let m = month ?? 1
        let d = day ?? 1
        self.date = "\(year)-\(m < 10 ? "0\(m)" : "\(m)")-\(d < 10 ? "0\(d)" : "\(d)")"
        self.dateEnum = dateEnum

        param.dateType = dateEnum.rawValue
        param.date = self.date
        
        fetchData()
    }
}

extension SecurityPersonStatisticsVC: BSPersonStiscControlDelegate {
    func handleSelected(_ dept: BalzByDeptWdModel?) {
        sticsV.reloadData(userName: dept?.lzrmc)

        if let ownerId = dept?.ownerId {
            let subVC = SecurityTopVC(withDeptId: deptId, deptName: deptName, date: date, dateEnum: dateEnum, ownerId: "\(ownerId)")
            navigationController?.pushViewController(subVC, animated: true)
        }
    }
}
